package org.example.controller;

import org.example.beans.RequestScopeBean;
import org.example.beans.SessionScopeBean;
import org.example.beans.ApplicationScopeBean;
import org.example.beans.SingletonBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Web作用域演示控制器
 * 演示Request、Session、Application作用域的Bean
 * 
 * 运行说明：
 * 1. 需要在Web容器（如Tomcat）中运行
 * 2. 可以通过配置DispatcherServlet来启用Web环境
 * 3. 访问不同的URL来观察不同作用域Bean的行为
 */
@Controller
@RequestMapping("/scope")
public class WebScopeController {
    
    @Autowired
    private SingletonBean singletonBean;
    
    @Autowired
    private RequestScopeBean requestScopeBean;
    
    @Autowired
    private SessionScopeBean sessionScopeBean;
    
    @Autowired
    private ApplicationScopeBean applicationScopeBean;
    
    /**
     * 测试所有Web作用域
     */
    @RequestMapping("/test")
    @ResponseBody
    public String testWebScopes(HttpServletRequest request) {
        StringBuilder sb = new StringBuilder();
        sb.append("<h2>Spring Web作用域演示</h2>");
        
        // 获取请求和会话信息
        HttpSession session = request.getSession();
        
        sb.append("<h3>请求信息</h3>");
        sb.append("请求ID: ").append(request.hashCode()).append("<br>");
        sb.append("会话ID: ").append(session.getId()).append("<br>");
        
        // 显示各个作用域Bean的实例ID
        sb.append("<h3>Bean实例信息</h3>");
        sb.append("Singleton Bean ID: ").append(singletonBean.getInstanceId()).append("<br>");
        sb.append("Request Bean ID: ").append(requestScopeBean.getInstanceId()).append("<br>");
        sb.append("Session Bean ID: ").append(sessionScopeBean.getInstanceId()).append("<br>");
        sb.append("Application Bean ID: ").append(applicationScopeBean.getInstanceId()).append("<br>");
        
        // 执行Bean方法
        singletonBean.doSomething();
        requestScopeBean.doSomething();
        sessionScopeBean.doSomething();
        applicationScopeBean.doSomething();
        
        sb.append("<h3>作用域说明</h3>");
        sb.append("<ul>");
        sb.append("<li><b>Singleton</b>: 在整个应用中始终是同一个实例</li>");
        sb.append("<li><b>Request</b>: 每次HTTP请求创建新实例，刷新页面会看到新的ID</li>");
        sb.append("<li><b>Session</b>: 每个会话创建一个实例，在同一浏览器会话中ID保持不变</li>");
        sb.append("<li><b>Application</b>: 整个Web应用创建一个实例，类似Singleton但在ServletContext级别</li>");
        sb.append("</ul>");
        
        sb.append("<p><a href='/scope/test'>刷新页面测试</a></p>");
        
        return sb.toString();
    }
    
    /**
     * 单独测试Request作用域
     */
    @RequestMapping("/request")
    @ResponseBody
    public String testRequestScope(HttpServletRequest request) {
        return "<h3>Request作用域测试</h3>" +
               "请求ID: " + request.hashCode() + "<br>" +
               "Request Bean ID: " + requestScopeBean.getInstanceId() + "<br>" +
               "<p>每次刷新都会创建新的Request Bean实例</p>" +
               "<a href='/scope/request'>刷新测试</a>";
    }
    
    /**
     * 单独测试Session作用域
     */
    @RequestMapping("/session")
    @ResponseBody
    public String testSessionScope(HttpSession session) {
        return "<h3>Session作用域测试</h3>" +
               "会话ID: " + session.getId() + "<br>" +
               "Session Bean ID: " + sessionScopeBean.getInstanceId() + "<br>" +
               "<p>在同一会话中Session Bean实例保持不变，新建会话才会创建新实例</p>" +
               "<a href='/scope/session'>刷新测试</a>";
    }
} 